package Cursus.Listeners;

import javax.swing.*;
import java.awt.*;

public class SwingComp extends JFrame {
    JButton but = new JButton("druk");
    JLabel lab = new JLabel("tekst");
    JPanel panel = new JPanel();
    JButton but2 = new JButton("druk2");

    DefaultListModel dlm = new DefaultListModel();
    JList jl = new JList(dlm);

    public SwingComp() {
        Container c = this.getContentPane();
        c.setLayout(new FlowLayout());
        c.add(but);
        c.add(lab);
        c.add(jl);
        panel.add(but2);
        panel.setBorder(new EigenBorder());
        panel.setPreferredSize(new Dimension(120,100));
        c.add(panel);
        setSize(200, 180);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        dlm.addElement(("--alle componenten van contentPane--"));
        Component [] comps = c.getComponents();
        for(int i = 0; i < comps.length; i++) {
            Class klasse = comps[i].getClass();
            dlm.addElement(klasse.getName());
        }
        dlm.addElement("--vader en grootvader van but2--");
        dlm.addElement("vader: " + but2.getParent().getClass().getName());
        dlm.addElement("grootvader: " + but2.getParent().getParent().getClass().getName());
        dlm.addElement("--alle voorouders van but2--");
        Component comp = but2;
        for(int i = 0; comp != null; i++) {
            dlm.addElement("vader " + i + " van but2: " + comp.getClass().getName());
            comp = comp.getParent();
        }
    }

    public static void main(String args[]) {
        new SwingComp();
    }
}
